package Tem;

/**
 * @author 陈Ser
 * @ClassName Tem2
 * @description: TODO
 * @Date 2021/9/10 18:35
 * @Version 1.0
 */
public class Tem2 {
    public static void main(String[] args) {
        int[] a = {1, 3, 4};//m
        int[] b = {2, 3, 6};//m<=n++
        int c = 4;
        System.out.println(find_kth(a, b, c));
    }

    public static int find_kth(int[] arr1, int[] arr2, int k) {
//        ArrayList<Integer> arrayList = new ArrayList<Integer>();

        long arrayIndex = 0;
        int[] array = new int[arr1.length + arr2.length];

        long i = 0, j = 0;
        for (; i < arr2.length || i < arr1.length; ) {
            if (arr1[Math.toIntExact(i)] < arr2[Math.toIntExact(j)]) {
//                arrayList.add(arr1[Math.toIntExact(i)]);
                array[Math.toIntExact(arrayIndex)] = arr1[Math.toIntExact(i)];
                arrayIndex++;
                i++;
            } else if (arr1[Math.toIntExact(i)] >= arr2[Math.toIntExact(j)]) {
//                arrayList.add(arr2[Math.toIntExact(j)]);
                array[Math.toIntExact(arrayIndex)] = arr2[Math.toIntExact(j)];
                arrayIndex++;
                j++;
            }
        }
        if (i == arr1.length) {
            for (; j < arr2.length; j++) {
//                arrayList.add(arr2[Math.toIntExact(j)]);
                array[Math.toIntExact(arrayIndex)] = arr2[Math.toIntExact(j)];
                arrayIndex++;
            }
        } else if (j == arr2.length) {
            for (; i < arr1.length; i++) {
//                arrayList.add(arr1[Math.toIntExact(i)]);
                array[Math.toIntExact(arrayIndex)] = arr1[Math.toIntExact(i)];
                arrayIndex++;
            }
        }

        return array[k - 1];
//        return arrayList.get(k-1);
    }
}
